// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.characters.traits;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import zombie.util.Lambda;
import zombie.util.StringUtils;
import zombie.util.list.PZArrayUtil;

public class TraitCollection {
    private final List<String> m_activeTraitNames = new ArrayList();
    private final List<TraitCollection.TraitSlot> m_traits = new ArrayList();

    public boolean remove(Object o) {
        return this.remove(String.valueOf(o));
    }

    public boolean remove(String name) {
        int _int = this.indexOfTrait(name);
        if (_int > -1) {
            this.deactivateTraitSlot(_int);
        }

        return _int > -1;
    }

    public void addAll(Collection<? extends String> c) {
        PZArrayUtil.forEach(c, this::add);
    }

    public void removeAll(Collection<?> c) {
        PZArrayUtil.forEach(c, this::remove);
    }

    public void clear() {
        PZArrayUtil.forEach(this.m_traits, traitSlot -> traitSlot.m_isSet = false);
        this.m_activeTraitNames.clear();
    }

    public int size() {
        return this.m_activeTraitNames.size();
    }

    public boolean isEmpty() {
        return this.m_activeTraitNames.isEmpty();
    }

    public boolean contains(Object o) {
        return this.contains(String.valueOf(o));
    }

    public boolean contains(String trait) {
        int _int = this.indexOfTrait(trait);
        return _int > -1 && this.getSlotInternal(_int).m_isSet;
    }

    public void add(String trait) {
        if (trait != null) {
            this.getOrCreateSlotInternal(trait).m_isSet = true;
            this.m_activeTraitNames.add(trait);
        }
    }

    public String get(int n) {
        return (String)this.m_activeTraitNames.get(n);
    }

    public void set(String name, boolean val) {
        if (val) {
            this.add(name);
        } else {
            this.remove(name);
        }
    }

    public TraitCollection.TraitSlot getTraitSlot(String name) {
        return StringUtils.isNullOrWhitespace(name) ? null : this.getOrCreateSlotInternal(name);
    }

    private int indexOfTrait(String string) {
        return PZArrayUtil.indexOf(this.m_traits, Lambda.predicate(string, TraitCollection.TraitSlot::isName));
    }

    private TraitCollection.TraitSlot getSlotInternal(int _int) {
        return (TraitCollection.TraitSlot)this.m_traits.get(_int);
    }

    private TraitCollection.TraitSlot getOrCreateSlotInternal(String string) {
        int _int = this.indexOfTrait(string);
        if (_int == -1) {
            _int = this.m_traits.size();
            this.m_traits.add(new TraitCollection.TraitSlot(string));
        }

        return this.getSlotInternal(_int);
    }

    private void deactivateTraitSlot(int int0) {
        TraitCollection.TraitSlot traitSlot = this.getSlotInternal(int0);
        traitSlot.m_isSet = false;
        int int1 = PZArrayUtil.indexOf(this.m_activeTraitNames, Lambda.predicate(traitSlot.Name, String::equalsIgnoreCase));
        if (int1 != -1) {
            this.m_activeTraitNames.remove(int1);
        }
    }

    public String toString() {
        return "TraitCollection(" + PZArrayUtil.arrayToString(this.m_activeTraitNames, "", "", ", ") + ")";
    }

    public class TraitSlot {
        public final String Name;
        private boolean m_isSet;

        private TraitSlot(String string) {
            this.Name = string;
            this.m_isSet = false;
        }

        public boolean isName(String string) {
            return StringUtils.equalsIgnoreCase(this.Name, string);
        }

        public boolean isSet() {
            return this.m_isSet;
        }

        public void set(boolean _boolean) {
            if (this.m_isSet != _boolean) {
                TraitCollection.this.set(this.Name, _boolean);
            }
        }

        public String toString() {
            return "TraitSlot(" + this.Name + ":" + this.m_isSet + ")";
        }
    }
}
